package com.slz3.business.module.drawManagement.draParticipateMember.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.drawManagement.draParticipateMember.domain.DraParticipateMember;
import com.slz3.business.module.drawManagement.draParticipateMember.domain.DraParticipateMemberFactory;
import com.slz3.business.module.drawManagement.draParticipateMember.steward.DraParticipateMemberSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class DraParticipateMemberServiceImpl implements DraParticipateMemberService {
    @Override
    public DraParticipateMember save(DraParticipateMember draParticipateMember) {
        return Repositorys.draParticipateMember.save(DraParticipateMemberFactory.initBaseFields(draParticipateMember));
    }

    @Override
    public Page<DraParticipateMember> page(MultiValueMap<String, String> params) {
        Page<DraParticipateMember> draParticipateMemberPage = Repositorys.draParticipateMember.findAll(
                DraParticipateMemberSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return draParticipateMemberPage;
    }


    @Override
    public DraParticipateMember modify(DraParticipateMember draParticipateMemberNew) {
        return Repositorys.draParticipateMember.saveAndFlush(DraParticipateMemberFactory.initBaseFields(draParticipateMemberNew));
    }

    @Override
    public Optional<DraParticipateMember> single(UUID uuid) {
        return Repositorys.draParticipateMember.findById(uuid);
    }

    @Override
    public List<DraParticipateMember> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.draParticipateMember.findAllById(uuids);
    }

    @Override
    public List<DraParticipateMember> delete(List<DraParticipateMember> draParticipateMemberList) {
        draParticipateMemberList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            DraParticipateMemberFactory.initBaseFields(item);
        });
        return Services.draParticipateMemberTransactional.batchModify(draParticipateMemberList);
    }

}
